package com.ut.demo.adapter;

import com.ut.demo.ResourceTable;
import com.ut.demo.util.ComponentUtil;
import com.dingmouren.paletteimageview.PaletteImageView;
import com.dingmouren.paletteimageview.factory.PixelMapFactory;
import com.dingmouren.paletteimageview.listner.OnParseColorListener;
import com.dingmouren.paletteimageview.util.ColorUtil;
import com.dingmouren.paletteimageview.util.JsonUtil;
import com.dingmouren.paletteimageview.util.LogUtil;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PictureListProvider extends BaseItemProvider {

    private Context context;

    private int[] pictures;

    public PictureListProvider(Context context, int[] pictures) {
        this.context = context;
        this.pictures = pictures;
    }

    @Override
    public int getCount() {
        return pictures==null ? 0 : pictures.length/2+(pictures.length%2);
    }

    @Override
    public Object getItem(int i) {
        return pictures[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder holder;
        if (component==null){
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_layout_of_picture_list, null, false);
            holder = new ViewHolder(component);
            component.setTag(holder);
        }else {
            holder = (ViewHolder) component.getTag();
        }
        PixelMap pixelMap = PixelMapFactory.CreatePixelMap(context, pictures[2*i]);
        holder.image.setPixelMap(pixelMap);
        if (2*i+1<pictures.length){
            PixelMap pixelMap_ = PixelMapFactory.CreatePixelMap(context, pictures[2*i+1]);
            holder.image_.setPixelMap(pixelMap_);
            holder.bottomContainer_.setVisibility(Component.VISIBLE);
        }else {
            holder.bottomContainer_.setVisibility(Component.INVISIBLE);
        }
//        LogUtil.i("create component, pixel==null: %b", pixelMap==null);
        return component;
    }



    public static class ViewHolder{

        public PaletteImageView image, image_;
        public Text title, content, title_, content_;
        public DirectionalLayout bottomContainer, bottomContainer_;

        public ViewHolder(Component component) {
            this.image = ComponentUtil.findComponent(component, ResourceTable.Id_image);
            this.title = ComponentUtil.findComponent(component, ResourceTable.Id_title);
            this.content = ComponentUtil.findComponent(component, ResourceTable.Id_content);
            this.bottomContainer = ComponentUtil.findComponent(component, ResourceTable.Id_bottom_container);
            this.image_ = ComponentUtil.findComponent(component, ResourceTable.Id_image_);
            this.title_ = ComponentUtil.findComponent(component, ResourceTable.Id_title_);
            this.content_ = ComponentUtil.findComponent(component, ResourceTable.Id_content_);
            this.bottomContainer_ = ComponentUtil.findComponent(component, ResourceTable.Id_bottom_container_);
            initListener(this);
        }

        private void initListener(ViewHolder holder) {
            holder.image.setOnParseColorListener(new OnParseColorListener() {
                @Override
                public void onComplete(PaletteImageView paletteImageView) {
                    int[] vibrant = paletteImageView.getVibrantColor();
                    int[] vibrantDark = paletteImageView.getDarkVibrantColor();
                    int[] vibrantLight = paletteImageView.getLightVibrantColor();
                    int[] muted = paletteImageView.getMutedColor();
                    int[] mutedDark = paletteImageView.getDarkMutedColor();
                    int[] mutedLight = paletteImageView.getLightMutedColor();
                    List<int[]> list = new ArrayList<>();
                    if (vibrant!=null&&vibrant.length>0)list.add(vibrant);
                    if (vibrantDark!=null&&vibrantDark.length>0)list.add(vibrantDark);
                    if (vibrantLight!=null&&vibrantLight.length>0)list.add(vibrantLight);
                    if (muted!=null&&muted.length>0)list.add(muted);
                    if (mutedDark!=null&&mutedDark.length>0)list.add(mutedDark);
                    if (mutedLight!=null&&mutedLight.length>0)list.add(mutedLight);
                    int[] array = list.get(new Random().nextInt(list.size()));
                    LogUtil.i("color array: %s", JsonUtil.toJson(list));
                    if(array != null) {
                        holder.title.setTextColor(new Color(Color.argb(0xff, ColorUtil.red(array[1]), ColorUtil.green(array[1]), ColorUtil.blue(array[1]))));
                        holder.content.setTextColor(new Color(Color.argb(0xff, ColorUtil.red(array[0]), ColorUtil.green(array[0]), ColorUtil.blue(array[0]))));
//                        holder.title.setTextColor(new Color(ColorUtil.argb2rgba(array[1])));
//                        holder.content.setTextColor(new Color(ColorUtil.argb2rgba(array[0])));
                        ShapeElement element = new ShapeElement();
                        element.setRgbColor(new RgbColor(ColorUtil.argb2rgba(array[2])));
                        element.setBounds(0, 0, holder.bottomContainer.getRight(), holder.bottomContainer.getBottom());
//                        element.setRgbColor(new RgbColor(array[2]));
                        holder.bottomContainer.setBackground(element);
//                        LogUtil.i("background color: %s", JsonUtil.toJson(element.getRgbColors()));
//
//                        LogUtil.i("title color: %s, content color: %s, background color: %s", Integer.toHexString(array[1]), Integer.toHexString(array[0]), Integer.toHexString(array[2]));
//                        LogUtil.i("title color: %s, content color: %s", JsonUtil.toJson(holder.title.getTextColor()), JsonUtil.toJson(holder.content.getTextColor()));
                    }
                }

                @Override
                public void onFail() {
                    LogUtil.i("ParseColor onFail");
                }
            });
            holder.image_.setOnParseColorListener(new OnParseColorListener() {
                @Override
                public void onComplete(PaletteImageView paletteImageView) {
                    int[] vibrant = paletteImageView.getVibrantColor();
                    int[] vibrantDark = paletteImageView.getDarkVibrantColor();
                    int[] vibrantLight = paletteImageView.getLightVibrantColor();
                    int[] muted = paletteImageView.getMutedColor();
                    int[] mutedDark = paletteImageView.getDarkMutedColor();
                    int[] mutedLight = paletteImageView.getLightMutedColor();
                    List<int[]> list = new ArrayList<>();
                    if (vibrant!=null&&vibrant.length>0)list.add(vibrant);
                    if (vibrantDark!=null&&vibrantDark.length>0)list.add(vibrantDark);
                    if (vibrantLight!=null&&vibrantLight.length>0)list.add(vibrantLight);
                    if (muted!=null&&muted.length>0)list.add(muted);
                    if (mutedDark!=null&&mutedDark.length>0)list.add(mutedDark);
                    if (mutedLight!=null&&mutedLight.length>0)list.add(mutedLight);
                    int[] array = list.get(new Random().nextInt(list.size()-1));
                    if(array != null) {
                        holder.title_.setTextColor(new Color(Color.argb(0xff, ColorUtil.red(array[1]), ColorUtil.green(array[1]), ColorUtil.blue(array[1]))));
                        holder.content_.setTextColor(new Color(Color.argb(0xff, ColorUtil.red(array[0]), ColorUtil.green(array[0]), ColorUtil.blue(array[0]))));
                        ShapeElement element = new ShapeElement();
                        element.setRgbColor(new RgbColor(ColorUtil.argb2rgba(array[2])));
                        element.setBounds(0, 0, holder.bottomContainer_.getRight(), holder.bottomContainer_.getBottom());
                        holder.bottomContainer_.setBackground(element);
                    }
                }

                @Override
                public void onFail() {}
            });
        }
    }


}
